package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderSearchVO;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {

    void insert(Orders orders);

    Page<Orders> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO);

    @Select("select * from orders where id = #{id}")
    List<Orders> getById(Long id);

    @Select("select count(0) from orders where status = #{status}")
    Integer countByStatus(Integer status);

    @Update("update orders set status = #{status} where id = #{id}")
    void updateStatus(Long id, Integer status);
    @Update("update orders set cancel_reason = #{cancelReason} where id = #{id}")
    void updatecancelReason(Long id, String cancelReason);
    @Update("update orders set rejection_reason = #{rejectionReason} where id = #{id}")
    void updateRejectionReason(Long id, String rejectionReason);

    Page<OrderSearchVO> conditionSearchWithUserId(OrdersPageQueryDTO ordersPageQueryDTO);
    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    @Update("update orders set status = #{status} where status = #{originStatus} and order_time < #{time}")
    void updateWrongStatus(Integer originStatus, Integer status, LocalDateTime time);

    @MapKey("date") // 指定使用结果中的date字段作为Map的key
    List<Map<String, BigDecimal>> sumByDate(
            @Param("begin") LocalDate begin,
            @Param("end") LocalDate end,
            @Param("status") Integer status
    );

    @MapKey("date")
    List<Map<String, Long>> getOrdersStatistics(LocalDate begin, LocalDate end, Integer status);

    @MapKey("date")
    List<Map<String, Long>> getNewUserCount(LocalDate begin, LocalDate end);

    @Select("select count(0) from user where create_time < #{localDate}")
    Long getTotalUserCount(LocalDate localDate);

    @MapKey("name")
    List<Map<String, Object>> sumByDish(LocalDate begin, LocalDate end ,  Integer status);

    Integer countByMap(Map map);

    Double sumByMap(Map map);
}
